//
//  MainViewController.m
//  ListMenu
//
//  Created by hzbj on 15/11/16.
//  Copyright © 2015年 hzbj. All rights reserved.
//

#import "MainViewController.h"



#import "ZFPopupMenu.h"
#import "ZFPopupMenuItem.h"

#import "HZDropDownMenu.h"


@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * myTableView;

@property (nonatomic, strong) UISegmentedControl * segmentControl;


@property (nonatomic, assign) BOOL  isSHowDropDown;

@end

@implementation MainViewController




-(NSArray *)menuItems
{
    ZFPopupMenuItem *item1 = [ZFPopupMenuItem initWithMenuName:@"新建"
                                                         image:[UIImage imageNamed:@"图标1"]
                                                        action:@selector(test1)
                                                        target:self];
    

    ZFPopupMenuItem *item2 = [ZFPopupMenuItem initWithMenuName:@"新建测试占位符"
                                                         image:[UIImage imageNamed:@"图标1"]
                                                        action:@selector(test1)
                                                        target:self];
    /*
    ZFPopupMenuItem *item3 = [ZFPopupMenuItem initWithMenuName:@"新建占位符"
                                                         image:[UIImage imageNamed:@"图标3"]
                                                        action:@selector(test3)
                                                        target:self];
    ZFPopupMenuItem *item4 = [ZFPopupMenuItem initWithMenuName:@"新建占位符占位符"
                                                         image:[UIImage imageNamed:@"图标4"]
                                                        action:@selector(test4)
                                                        target:self];
 */
    
    return @[item1,item2];//,item3,item4,item2,item4,item3,item2,item3,item4];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"下拉列表";
    
    self.isSHowDropDown = NO;

     self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"国家",@"人民"]];
     self.segmentControl.frame = CGRectMake(0, 0, 80, 30);
    [self.segmentControl addTarget:self action:@selector(segmentIndex:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = self.segmentControl;
    
    
 
    
    UIImage* img=[UIImage imageNamed:@"图标1"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =CGRectMake(0, 0, 32, 32);
    [btn setBackgroundImage:img forState:UIControlStateNormal];
    [btn addTarget: self action: @selector(barSelectView:) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem* item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem=item;
    
    
    
    
    
    UIImage* imgLeft=[UIImage imageNamed:@"图标1"];
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLeft.frame =CGRectMake(0, 0, 32, 32);
    [btnLeft setBackgroundImage:imgLeft forState:UIControlStateNormal];
    [btnLeft addTarget: self action: @selector(barSelectViewleft:) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem* itemLeft=[[UIBarButtonItem alloc]initWithCustomView:btnLeft];
    self.navigationItem.leftBarButtonItem=itemLeft;
    
    
    
    /*®
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
     */
    
    UIButton *LeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    LeftButton.titleLabel.numberOfLines = 0;
    LeftButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    LeftButton.titleLabel.font = [UIFont systemFontOfSize:13.0];
    LeftButton.layer.borderWidth = 1;
    [LeftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [LeftButton setFrame:CGRectMake(10, 150 , 40, 40)];
    //      [LeftButton setTitle:@"默认横向布局" forState:UIControlStateNormal];
    [LeftButton addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:LeftButton];
    
    
}

-(void)barSelectViewleft:(UIButton *)sender {
    ZFPopupMenu *popupMenu = [[ZFPopupMenu alloc] initWithItems:[self menuItems]];
    NSLog(@"--------------------%@",NSStringFromCGRect(sender.frame));
    [popupMenu showInView:self.navigationController.view fromRect:sender.frame layoutType:Vertical];
    [self.navigationController.view addSubview:popupMenu];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString * CellID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    cell.textLabel.text = @"中间";
    return cell;
}


-(void)showMenu:(UIButton *)sender {
   
    ZFPopupMenu *popupMenu = [[ZFPopupMenu alloc] initWithItems:[self menuItems]];
    NSLog(@"--------------------%@",NSStringFromCGRect(sender.frame));
    [popupMenu showInView:self.navigationController.view fromRect:sender.frame layoutType:Vertical];
    [self.navigationController.view addSubview:popupMenu];
}



-(void)test1
{
    NSLog(@"新建");
}
/** 显示 */
- (void)showFrom {
    
    /*
    // 获得最上面的窗口
    UIWindow * window = [[UIApplication sharedApplication].windows lastObject];
    // 添加自己到窗口上
    [window addSubview:self.view];
    //window.backgroundColor = [UIColor grayColor];
    
    // 设置尺寸
    window.bounds = self.view.frame;
    // 调整containerView的位置
    // 默认情况下，frame是以父控件左上角为坐标原点
    // 可以转换坐标系原点，改变frame的参照点
    CGRect newFrame = [self.view convertRect:self.view.frame toView:window];
    NSLog(@"%@",NSStringFromCGRect(newFrame));
    
    UIView * showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 300)];
    showView.backgroundColor = [UIColor grayColor];
    [self.navigationController.view addSubview:showView];
    */
    
    
    HZDropDownMenu * menu = [HZDropDownMenu menu];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    menu.content = tableView;
    // 添加下拉菜单
    [menu showFrom:self.segmentControl];

    
}


-(void)barSelectView:(UIButton *)sender {
    
    HZDropDownMenu * menu = [HZDropDownMenu menu];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    menu.content = tableView;
    // 添加下拉菜单
    [menu showFrom:sender];
}






-(void)segmentIndex:(UISegmentedControl *)seng {
    HZDropDownMenu * menu = [HZDropDownMenu menu];
    if (!self.isSHowDropDown) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        menu.content = tableView;
        // 添加下拉菜单
        [menu showFrom:seng];
    }
    //self.isSHowDropDown = YES;
    else{
        [menu dismiss];
    }
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
